<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core">

 <ui:composition>

     <script >

      //<![CDATA[
      function mascaraTexto(evento, mascara){

    var campo, valor, i, tam, caracter;

    if (document.all) // Internet Explorer
       campo = evento.srcElement;
    else // Nestcape, Mozzila
        campo= evento.target;

    valor = campo.value;
    tam = valor.length;

    for(i=0;i<mascara.length;i++){
       caracter = mascara.charAt(i);
       if(caracter!="9")
          if(i<tam & caracter!=valor.charAt(i))
             campo.value = valor.substring(0,i) + caracter + valor.substring(i,tam);


    }

 }
 function retornaCPFCNPJFormatado(Campo, tipo){
	var vTipo = tipo;
	var vr = new String(Campo.value);
	
	if (vr != ""){
		if (vTipo == 1){
			vr = vr.replace(".", "");
			vr = vr.replace(".", "");
			vr = vr.replace("-", "");
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,2);
		} else {
			vr = vr.replace(".", "");
			vr = vr.replace(".", "");
			vr = vr.replace("*", "");
			vr = vr.replace("-", "");		
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
		}
	}
}

function extractNumbers(s){
	var r = '';
	for(i = 0 ; i < s.length ; i++){
	   if (!isNaN(s.charAt(i)))
	       r = r + s.charAt(i);
    }
    return r;
}

function retornaDocumentoFormatado(evento){
    var Campo;

    if (document.all) // Internet Explorer
       Campo = evento.srcElement;
    else // Nestcape, Mozzila
        Campo = evento.target;

	documento = extractNumbers(Campo.value);
	if (documento.length == 11){
		retornaCPFCNPJFormatado(Campo, 1);
	}else if (documento.length == 14){
		retornaCPFCNPJFormatado(Campo, 2);
	}
}



        //]]>
    </script>
<c:if test="#{tipo == 'cpf'}">
 <h:inputText id="#{id}" value="#{value}" maxlength="14" size="14" onkeyup="return mascaraTexto(event,'999.999.999-99')">
      
  </h:inputText>
</c:if>
<c:if test="#{tipo == 'cnpj'}">
 <h:inputText id="#{id}" value="#{value}" maxlength="18" size="18" onkeyup="return mascaraTexto(event,'99.999.999*9999-99')">
      
  </h:inputText>
</c:if>
<c:if test="#{tipo == 'cpfCnpj'}">
    <h:inputText id="#{id}" value="#{value}" maxlength="18" size="18" onkeyup="return retornaDocumentoFormatado(event)">
      
  </h:inputText>
</c:if>
<c:if test="#{(tipo != 'cpfCnpj') and (tipo !='cpf') and (tipo !='cnpj')}">
    <h:outputText value="ERRO - parametro(to sem acento) inválido." id="erroComp12354" />!!! adicionar link da documentaçao
</c:if>
   
  </ui:composition>

</html>
